package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.reggie.common.R;
import com.heima.reggie.common.Thutil;
import com.heima.reggie.pojo.Dish;
import com.heima.reggie.pojo.ShoppingCart;
import com.heima.reggie.service.DishFlavorService;
import com.heima.reggie.service.ShoppingCarService;
import com.sun.prism.impl.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCarController {
    @Autowired
    private ShoppingCarService shoppingCarService;

    @GetMapping("/list")
    public R<List> list() {
        Long id = Thutil.getThreadLocal();
        LambdaQueryWrapper<ShoppingCart> splamd = new LambdaQueryWrapper<>();
        splamd.eq(ShoppingCart::getUserId, id);
        List<ShoppingCart> list = shoppingCarService.list(splamd);


        return R.success(list);
    }

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart) {
        Long userid = Thutil.getThreadLocal();
        shoppingCart.setUserId(userid);

        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lamd = new LambdaQueryWrapper<>();
        lamd.eq(ShoppingCart::getUserId, userid);
        // ShoppingCart one = shoppingCarService.getOne(lamd);
        Long dishId1 = shoppingCart.getDishId();
        if (dishId1 != null) {
            //判断是否为菜品
            lamd.eq(ShoppingCart::getDishId, dishId);
        } else {

            lamd.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart one1 = shoppingCarService.getOne(lamd);

        if (one1 != null) {
            log.info("-------------------{}", one1);
            Integer number = one1.getNumber() + 1;
            one1.setNumber(number);
            shoppingCarService.updateById(one1);

            return R.success(one1);
        } else {

            shoppingCart.setNumber(1);
            shoppingCarService.save(shoppingCart);


            return R.success(shoppingCart);
        }


    }
@PostMapping("/sub")
public  R sub( @RequestBody ShoppingCart shoppingCart  ){

    LambdaQueryWrapper<ShoppingCart> sopl = new LambdaQueryWrapper<>();
    Long user = Thutil.getThreadLocal();
    sopl.eq(ShoppingCart::getUserId,user);
    sopl.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());

    ShoppingCart one = shoppingCarService.getOne(sopl);
    Integer number = one.getNumber();
    if (number > 1) {
        one.setNumber(number-1);
        shoppingCarService.updateById(one);
        return  R.success(one);

    }
    shoppingCarService.removeById(one.getId());


    return R.success("123")  ;
};


    @DeleteMapping("/clean")
    public  R clean(){
        Long threadLocal = Thutil.getThreadLocal();
        LambdaQueryWrapper<ShoppingCart> sp = new LambdaQueryWrapper<>();
        sp.eq(ShoppingCart::getUserId,threadLocal);
        boolean remove = shoppingCarService.remove(sp);


        return R.success("清空成功+0+2------------------------");
    };

}
